//
//  ViewController.m
//  Reader
//
//  Created by 钟纪旺 on 16/12/7.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "ViewController.h"
#import "YBPopupMenu.h"
#import "FourTableView.h"
#import "Bookchat.h"
#import "ReadViewController.h"
#define TITLES @[@"充值", @"阅读历史", @"常见问题",@"联系客服",@"设置"]
#define ICONS  @[@"motify",@"delete",@"saoyisao",@"pay"]
@interface ViewController ()<YBPopupMenuDelegate,UITableViewDelegate,UITableViewDataSource>
{
    NSArray <NSURL *>*texts;
    NSArray <NSURL *>*imgs;
}

@property (weak, nonatomic) IBOutlet FourTableView *forTableView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *addItem;
@property (strong, nonatomic) NSArray *dataArr;
@end

@implementation ViewController

-(NSArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = @[@"[综]以貌取人",@"龙族1火之晨曦",@"龙族3黑月之潮（上中下）江南",@"龙族4奥丁之渊（完整）",@"爵迹",@"龙族II 悼亡者之瞳",@"妾色",@"血乌鸦",@"镜界之穹",@"爆笑腐女追君记"];
    }
    return _dataArr;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Bookchat *cell =[tableView dequeueReusableCellWithIdentifier:@"zhong" forIndexPath:indexPath];

    cell.logo.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg",self.dataArr[indexPath.row]]];
    cell.label.text = self.dataArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
//    btn.backgroundColor = [UIColor clearColor];
//    NSInteger row = indexPath.row;
//    NSInteger index = row ;
//    btn.tag = 100 +index;
//    [btn addTarget:self action:@selector(gotoBook:) forControlEvents:UIControlEventTouchDown];
//    [cell addSubview:btn];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:_dataArr[indexPath.row] ofType:@"txt"];
    NSString *content = [[NSString alloc]initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    
    ReadViewController *readVC = [[ReadViewController alloc]init];
    
    readVC.text = content;
    readVC.hidesBottomBarWhenPushed = YES;
    //[self.navigationController pushViewController:readVC animated:YES];
    readVC.modalTransitionStyle =  UIModalTransitionStyleCrossDissolve;
    [self presentViewController:readVC animated:YES completion:nil];
    
    //readVC.modalTransitionStyle =  UIModalTransitionStyleCrossDissolve;
    //[self presentViewController:readVC animated:YES completion:nil];
}



- (IBAction)onButton:(UIButton *)sender {
    
    _scrollview.contentOffset = CGPointMake(sender.tag *self.view.frame.size.width, 0);
    
}

- (IBAction)onD:(UIButton *)sender {
    
     [YBPopupMenu showRelyOnView:sender titles:TITLES icons:ICONS menuWidth:120 delegate:self];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSBundle *bundle = [NSBundle mainBundle];
    imgs = [bundle URLsForResourcesWithExtension:@"jpg" subdirectory:nil];
    texts = [bundle URLsForResourcesWithExtension:@"txt" subdirectory:nil];
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hei"]]];
    
    UIButton *tmpBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    tmpBtn.frame = CGRectMake(0, 0, 25, 25);
    tmpBtn.tintColor = [UIColor whiteColor];
    [tmpBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [_addItem setCustomView:tmpBtn];
    
    _scrollview.scrollEnabled = YES;
   [_scrollview setContentSize:CGSizeMake(self.view.frame.size.width*4, self.view.frame.size.height)];
    self.forTableView.delegate = self;
    self.forTableView.dataSource = self;

    

}
-(void)viewDidAppear:(BOOL)animated

{
    
    _scrollview.contentSize =CGSizeMake([UIScreen mainScreen].bounds.size.width*4,0);
    _scrollview.pagingEnabled = YES;
    _scrollview.bounces = NO;
    
}
-(void)btnClicked:(UIButton *)sender
{
     [YBPopupMenu showRelyOnView:sender titles:TITLES icons:ICONS menuWidth:120 delegate:self];
}

#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu
{
    NSLog(@"点击了 %@ 选项",TITLES[index]);
}
//下导航消失
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

////下导航出现
//- (void)viewWillDisappear:(BOOL)animated {
//    
//    [super viewWillDisappear:animated];
//    [super viewWillAppear:animated];
//    //    self.navigationController.tabBarController.tabBar.hidden = NO;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
